import { User } from "../../../modeal/user.js"
const user = new User()
Page({
  data: {
    tabs: [
      { id: 0, name: '代言名片' },
      { id: 1, name: '佣金明细' }
    ],
    currentIndex: 0,
    userinfo: {},
    spokesmanInfo: {},
    fans: [],
    page: 1,
    statusText: '您暂时没有粉丝'
  },
  onLoad: function () {
    let userinfo = wx.getStorageSync("userinfo")
    let spokesmanInfo = wx.getStorageSync("spokesmanInfo")
    this.setData({ userinfo, spokesmanInfo })
  },
  onShow: function () {
    this.getMyFansBrokerage();   // 获的粉丝收益
  },

  // 获的粉丝收益
  getMyFansBrokerage () {
    let page = this.data.page
    user.getMyFansBrokerage(page).then(res => {
      console.log(res);
      this.setData({ fans: res })
    })
  },

  // 保存二维码在本地
  savePic () {
    let qrcode_path = this.data.spokesmanInfo.qrcode_path
    wx.showLoading({
      title: '保存中...',
      mask: true,
    });
    wx.downloadFile({
      url: qrcode_path,
      success: res => {
        if (res.statusCode === 200) {
          let img = res.tempFilePath;
          wx.saveImageToPhotosAlbum({
            filePath: img,
            cuccess: res => {
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
              });
            },
            fail: err => {
              wx.showToast({
                title: '保存失败',
                duration: 2000
              });
            },
            complete: res => {
              wx.hideLoading()
            }
          })
        }
      },
      fail: err => {
        console.log(err, 123);

      }
    })

  },

  clickTab (e) {
    let currentIndex = e.target.dataset.id
    this.setData({ currentIndex })
    currentIndex == 1 ? this.getMyFansBrokerage() : ''
    // 修改顶部标题
    wx.setNavigationBarTitle({
      title: this.data.tabs[currentIndex].name
    })
  }
})